package sz.szsmk.citizencard.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.IUserInfo;
import sz.szsmk.citizencard.model.network.bean.response.QuerLeisureCardsInfoRes;
import sz.szsmk.citizencard.ui.adapter.LeisureYearCardsDialogAdapter;
import sz.szsmk.citizencard.util.ACache;

/**
 * Created by hyt on 2019/4/11.
 */
public class LeisureYearCardsDialog extends Dialog  {

    List<QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean> leisur_cards ;
    ILeisureYearCardsDialog iLeisureYearCardsDialog;

    ImageView iv_close;

    TextView tv_tip;

    RecyclerView rv_list;

    Button btn_cancel;

    Button btn_ok;

    QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean lcTemp;

    LeisureYearCardsDialogAdapter adapter;
    public LeisureYearCardsDialog(@NonNull Context context, ILeisureYearCardsDialog iLeisureYearCardsDialog,
                                  List<QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean> leisur_cards) {
        this(context, R.style.dialog);

        this.leisur_cards = leisur_cards;
        this.iLeisureYearCardsDialog = iLeisureYearCardsDialog;

        int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
        View divider = findViewById(divierId);
        divider.setVisibility(View.GONE);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(LayoutInflater.from(getContext()).inflate(R.layout.dialog_leisureyearcards, null));
        iv_close = findViewById(R.id.iv_close);
        tv_tip = findViewById(R.id.tv_tip);
        rv_list  = findViewById(R.id.rv_list);

        btn_cancel  = findViewById(R.id.btn_cancel);
        btn_ok  = findViewById(R.id.btn_ok);


        initView();

    }
    private void initView(){

        adapter = new LeisureYearCardsDialogAdapter(getContext(),leisur_cards);
        rv_list.setLayoutManager(new LinearLayoutManager(getContext()));
        rv_list.setAdapter(adapter);
        adapter.setiLycardsAdatperCallBack(leisur_cardsInfoRes -> {

            lcTemp = leisur_cardsInfoRes;
            btn_ok.setText("确认");
            if(leisur_cardsInfoRes.getAcctType()==0){
                if(leisur_cardsInfoRes.getIsHavingPicture()!=1){
                    btn_ok.setText("添加头像");
                }
            }

        });

        String userinfo = ACache.get(getContext()).getAsString("userinfo");

        if(leisur_cards.size()==1){
            tv_tip.setText("您的休闲年卡卡号如下");
        }else{
            tv_tip.setText("请选择一张您的休闲年卡卡号，添加至身份码。");
        }

        btn_cancel.setOnClickListener(v ->
                LeisureYearCardsDialog.this.dismiss());
        iv_close.setOnClickListener(v->
                LeisureYearCardsDialog.this.dismiss());
        btn_ok.setOnClickListener(v -> {

            if(this.lcTemp==null){
                Toast.makeText(getContext(),"请选择一张休闲年卡",Toast.LENGTH_LONG).show();
            }else{

                if(this.lcTemp.getAcctType()==0){
                    if(this.lcTemp.getIsHavingPicture()!=1){
                        //上传头像
                        iLeisureYearCardsDialog.lyCardsCallBack(this.lcTemp,1);

                    }else{
                        //有头像 直接绑定
                        iLeisureYearCardsDialog.lyCardsCallBack(this.lcTemp,-1);
                    }
                }else{
                    //线上  直接绑定
                    iLeisureYearCardsDialog.lyCardsCallBack(this.lcTemp,-1);
                }
            }

        });
    }

    protected LeisureYearCardsDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, R.style.alertdialog);
    }

    public interface ILeisureYearCardsDialog{

        void lyCardsCallBack(QuerLeisureCardsInfoRes.QuerLeisureCardsInfoBean lcTemp,int type);
    }

}